﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace DienLib
{
    public class RegexD
    {
        /// <summary>
        /// Seperate input string into group of string by seperator provided
        /// </summary>
        /// <param name="input"></param>
        /// <param name="seperator"></param>
        /// <returns></returns>
        public static string[] Get2GroupSeperateBy(string input, string seperator)
        {
            string exp = @"^(.+)" + seperator + @"(.+)$";
            Match match = Regex.Match(input, exp);
            return new string[] { match.Groups[1].Value, match.Groups[2].Value };
        }

        /// <summary>
        /// Get Double from String contain single Double Number. Can have Non-Numeric number inside no problem.
        /// </summary>
        /// <param name="singleNum"></param>
        /// <returns></returns>
        public static double GetDoubleFromString(string singleNum)
        {
            Match match = Regex.Match(singleNum, @"(-?)([0-9]+)([,\.]?)([0-9]*)");
            if (!match.Success) throw new InvalidOperationException("Not a string Number");

            if (match.Groups[3].Value != string.Empty)
            {   // Co dau thap phan
                string txt = match.ToString().Replace(match.Groups[3].Value, NumberFormatInfo.CurrentInfo.NumberDecimalSeparator);
                return Convert.ToDouble(txt);
            }
            else return Convert.ToDouble(match.Groups[0].Value);
        }

        public static void test1()
        {
            string[] s = Regex.Split("I fell \"OK\" now \"Ha\" ka ka !", @"""[^""]*""");

            foreach (Match match in Regex.Matches("I fell \"OK\" now \"Ha\" ka ka !", @"""[^""]*"""))
            {
                string st = match.Value;
            }

            foreach (Match match in Regex.Matches("Yes. This dog is very friendly.", @"((\w+)[\s.])+"))
            {

            }
        }
    }
}
